package com.ebay.buyer;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.ebay.model.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ReviewBill extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	public int getProduct_id() {
		return product_id;
	}

	public void setProduct_id(int product_id) {
		this.product_id = product_id;
	}

	public int getCourierCharges() {
		return courierCharges;
	}

	public void setCourierCharges(int courierCharges) {
		this.courierCharges = courierCharges;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	public float getSubtotal() {
		return subtotal;
	}

	public void setSubtotal(float subtotal) {
		this.subtotal = subtotal;
	}

	public ArrayList<Book> getLstbooks() {
		return lstbooks;
	}

	public void setLstbooks(ArrayList<Book> lstbooks) {
		this.lstbooks = lstbooks;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public ArrayList<String> getShippingAddress() {
		return shippingAddress;
	}

	public void setShippingAddress(ArrayList<String> shippingAddress) {
		this.shippingAddress = shippingAddress;
	}
	
	public int getQuantitypurchased() {
		return quantitypurchased;
	}

	public void setQuantitypurchased(int quantitypurchased) {
		this.quantitypurchased = quantitypurchased;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<String> shippingAddress;
	private int user_id;
	private float subtotal;
	private String url = null;
	ArrayList<Book> lstbooks = new ArrayList<Book>();
	private int product_id;
	private int quantitypurchased;
	private int courierCharges;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public ReviewBill() {
		super();
	}

	@Override
	public void setServletResponse(HttpServletResponse res) {
		this.response = res;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;

	}

	public String execute()
	{
		Map<String,Object> session = ActionContext.getContext().getSession();

		//check if the user is logged in else, store the incoming url and redirect to sign in.
		if(session == null || session.get("loggedin") == null || session.get("user") == null || !((String) session.get("loggedin")).equals("true") ){
			url = "/"+ActionContext.getContext().getName()+"?"+request.getQueryString();
			System.out.println("URL : "+url); 
			return LOGIN;
		}
		System.out.println("in review"+product_id);

		Book b = Book.getBookDetails(product_id);
		subtotal = b.getPrice()*quantitypurchased;
		lstbooks.add(b);
		
		UserModel u = (UserModel)session.get("user");
		shippingAddress=u.getshippingaddress(u.getUser_id());

		System.out.println("Size "+shippingAddress.size());

		return SUCCESS;

	}
}
